<template>
    
    <div class="app-container">
        <el-card>
            <el-form ref="form1" :model="formData" label-width="100" :rules="rules">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="围栏名称" prop="fenceName">
                            <el-input v-model="formData.fenceName" style="width:50%"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="打卡类型" prop="fenceType">
                            <el-select v-model="formData.fenceType" style="width:50%">
                                <el-option label="宿舍打卡" value="1"></el-option>
                                <el-option label="班级打卡" value="2"></el-option>
                                <el-option label="考勤打卡" value="3"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="打卡时间">
                            <div v-for="(item,index) in formData.clockList" :key="item.index" style="margin-left: 68px;padding-bottom: 5px;">
                                <el-time-select
                                    v-model="item.clockBeginTime"
                                    placeholder="起始时间"
                                    value-format="HH:mm"
                                    :picker-options="{
                                    start: '06:00',
                                    step: '00:10',
                                    end: '23:30'
                                    }">
                                </el-time-select>
                                <el-time-select
                                    v-model="item.clockEndTime"
                                    placeholder="结束时间"
                                    value-format="HH:mm"
                                    :picker-options="{
                                    start: '06:00',
                                    step: '00:10',
                                    end: '23:30',
                                    minTime: item.clockBeginTime
                                    }">
                                </el-time-select>
                                <el-button icon="el-icon-plus"  @click="addTime" v-if="index==0"></el-button>
                                <el-button icon="el-icon-minus"  @click="subTime(index)" v-if="index>0"></el-button>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="有效状态">
                            <el-switch
                                v-model="formData.status"
                                active-value="1"
                                inactive-value="0">
                            </el-switch>
                        </el-form-item>
                    </el-col>
                </el-row>
                
                
            </el-form>
        </el-card>
        <el-row>
            <el-col :span="12">
                <span class="font">注：围栏信息要首选保存围栏信息后，才可以提交</span>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-button @click="createPolygon" icon="el-icon-edit" type="success" :disabled="polygon1.length>0">绘制围栏</el-button>
                <el-button @click="clearFence" icon="el-icon-refresh">重置围栏</el-button>
                <el-button @click="saveFence" icon="el-icon-document-add" type="warning" :disabled="polygon1.length>0">保存围栏信息</el-button>
            </el-col>
            <el-col :span="12" style="text-align:right">
                <el-button @click="submitFence" type="primary" icon="el-icon-finished">提交围栏信息</el-button>
                <el-button @click="goBack" icon="el-icon-refresh-right">返回</el-button>
            </el-col>
        </el-row>
        <el-card>
            <div id="container" class="container"></div>
        </el-card>

        
    </div>
        
</template>
    
<script>
import AMapLoader from "@amap/amap-jsapi-loader";
import { addOrUpdateConfig,fenceDetail } from "@/api/workmgr/locationFence.js";
window._AMapSecurityConfig = {
    // 安全密钥
    securityJsCode: "3684ef9a7571deaab4b328c25ffdce45",
};
    
export default {
    name: "locationFence",
    data() {
    return {
        // 地图实例
        map: null,
        AutoComplete:null,
        polyEditor:null,
        clickListener:null,
        lnglatList:[],
        polygon1:[],
        formData:{
            clockList:[
                {clockBeginTime: '08:00',clockEndTime:'10:00'}
            ]
        },
        rules:{
            fenceName:[{ required: true, message: "请填写围栏名称", trigger: "blur" }],
            fenceType:[{ required: true, message: "请选择围栏类型", trigger: "blur" }],
        }
    };
    },
    created() {
    
    },
    mounted() {
        this.init()
    },
    created(){
       
    },
    methods: {
        goBack(){
            this.$router.push("/studentManagement/locations/locationFence")
        },
        //提交围栏
        submitFence(){
            if(this.polygon1.length<=0){
                this.$message({ message: "请绘制围栏范围", type: "warning" });
                return 
            }
            this.$refs.form1.validate(valid => {
                if (valid) {
                    this.formData.locationList=this.polygon1
                    addOrUpdateConfig(this.formData).then(resp=>{
                        this.$message({ message: "提交成功", type: "success" });
                        this.$router.push("/studentManagement/locations/locationFence")
                    })
                }
            })
        },
        addTime(){
            this.formData.clockList.push({
                clockBeginTime: '08:00',clockEndTime:'10:00'
            })
        },
        subTime(index){
            this.formData.clockList.splice(index,1)
        },
        //地图展示
        init(){
            AMapLoader.load({
                "key": "7b5577c13418c4dd86bfb9a2bdbc0eaa",              // 申请好的Web端开发者Key，首次调用 load 时必填
                "version": "2.0",   // 指定要加载的 JS API 的版本，缺省时默认为 1.4.15
                "plugins": ["AMap.PolygonEditor",'AMap.ToolBar','AMap.MouseTool'],           // 需要使用的的插件列表，如比例尺'AMap.Scale'等
            }).then((AMap)=>{
                this.map = new AMap.Map('container',{
                    center: [116.471354, 39.994257],
                    zoom: 16.8
                });

                this.polyEditor = new AMap.PolygonEditor(this.map);
                  //编辑初始化数据
                if(this.$route.query.id){
                    this.getFanceData(this.$route.query.id)
                }
                
            }).catch(e => {
                console.log(e);
            })
        },
        getFanceData(id){
            //有ID则查询围栏经纬度
            if(id){
                fenceDetail(id).then(resp=>{
                    this.formData = resp.data
                    this.polygon1 = resp.data.locationList
                    this.map.add([new AMap.Polygon({
                        path: resp.data.locationList
                    })]);
                    this.map.setFitView();
                })
                
            }
        },
        
        createPolygon() {
            // this.saveFence()
            if(this.polygon1.length>0){
                this.$message({ message: "请重置围栏后再进行绘制围栏", type: "warning" });
                return
            }
            this.lnglatList=[]
            this.polyEditor.close();
            this.polyEditor.setTarget();
            this.polyEditor.open();
        },
        //清空围栏信息
        clearFence(){
            this.map.clearMap()
            this.map.remove(this.polygon1)
            //重置清空经纬度
            this.polygon1=[]

        },
        //点击页面设置围栏经纬度
        mapOnClick(e){
            this.lnglatList.push({
                latitude :e.lat, //纬度
                longitude: e.lng //经度
            })
            this.polygon1 = new AMap.Polygon({
                path: this.lnglatList
            })
            console.log(this.lnglatList)
        },
        //保存围栏信息
        saveFence(){
            this.polyEditor.close();
            //获取坐标点
            const list = this.polyEditor.getTarget().getPath().map((item, idx) => {
                return [item.KL, item.kT];
            });
            this.polygon1=list
            console.log(list)  
            
        }
    }
};
</script>
    
<style>
#container{
  padding:0px;
  margin: 0px;
  width: 100%;
  height: 800px;
}
.font{
    font-size: 14px;
    color: red;
}
</style>